{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus.model.hybrid import BGEM3EmbeddingFunction\n",
    "import pymilvus\n",
    "from pymilvus import (\n",
    "    utility,\n",
    "    FieldSchema, CollectionSchema, DataType,\n",
    "    Collection, AnnSearchRequest, RRFRanker, connections,\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "The attribute 'MilvusModelLoader' is not found in 'pymilvus.model'. This might be due to an outdated version of 'milvus_model'. For upgrading to the latest version, use 'pip install milvus-model --upgrade'. For more information, please visit https://github.com/milvus-io/milvus-model.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\pymilvus\\model\\__init__.py:12\u001b[0m, in \u001b[0;36mMilvusModelLoader.__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m---> 12\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_milvus_model\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     13\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'milvus_model' has no attribute 'MilvusModelLoader'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 15\u001b[0m\n\u001b[0;32m      9\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWho started AI research?\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;66;03m# BGE-M3 model can embed texts as dense and sparse vectors.\u001b[39;00m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;66;03m# It is included in the optional `model` module in pymilvus, to install it,\u001b[39;00m\n\u001b[0;32m     13\u001b[0m \u001b[38;5;66;03m# simply run \"pip install pymilvus[model]\".\u001b[39;00m\n\u001b[1;32m---> 15\u001b[0m bge_m3_ef \u001b[38;5;241m=\u001b[39m \u001b[43mpymilvus\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mMilvusModelLoader\u001b[49m\u001b[38;5;241m.\u001b[39m_milvus_model\u001b[38;5;241m.\u001b[39mhybrid\u001b[38;5;241m.\u001b[39mBGEM3EmbeddingFunction(use_fp16\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, device\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcpu\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     17\u001b[0m docs_embeddings \u001b[38;5;241m=\u001b[39m bge_m3_ef(docs)\n\u001b[0;32m     18\u001b[0m query_embeddings \u001b[38;5;241m=\u001b[39m bge_m3_ef([query])\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\pymilvus\\model\\__init__.py:20\u001b[0m, in \u001b[0;36mMilvusModelLoader.__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m     14\u001b[0m     err_str \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m     15\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe attribute \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m is not found in \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpymilvus.model\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     16\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis might be due to an outdated version of \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmilvus_model\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     17\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor upgrading to the latest version, use \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpip install milvus-model --upgrade\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     18\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor more information, please visit https://github.com/milvus-io/milvus-model.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     19\u001b[0m     )\n\u001b[1;32m---> 20\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(err_str) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
      "\u001b[1;31mAttributeError\u001b[0m: The attribute 'MilvusModelLoader' is not found in 'pymilvus.model'. This might be due to an outdated version of 'milvus_model'. For upgrading to the latest version, use 'pip install milvus-model --upgrade'. For more information, please visit https://github.com/milvus-io/milvus-model."
     ]
    }
   ],
   "source": [
    "import pymilvus.model\n",
    "\n",
    "\n",
    "docs = [\n",
    "    \"Artificial intelligence was founded as an academic discipline in 1956.\",\n",
    "    \"Alan Turing was the first person to conduct substantial research in AI.\",\n",
    "    \"Born in Maida Vale, London, Turing was raised in southern England.\",\n",
    "]\n",
    "query = \"Who started AI research?\"\n",
    "\n",
    "# BGE-M3 model can embed texts as dense and sparse vectors.\n",
    "# It is included in the optional `model` module in pymilvus, to install it,\n",
    "# simply run \"pip install pymilvus[model]\".\n",
    "\n",
    "bge_m3_ef = pymilvus.model.MilvusModelLoader._milvus_model.hybrid.BGEM3EmbeddingFunction(use_fp16=False, device=\"cpu\")\n",
    "\n",
    "docs_embeddings = bge_m3_ef(docs)\n",
    "query_embeddings = bge_m3_ef([query])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
